package com.example.sources.jdbc.proxy;

import com.example.sources.annotation.Query;
import com.example.sources.jdbc.tool.RegexUtil;
import com.example.sources.jdbc.tool.TypeUtil;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/18.
 */
public class DaoInvocationHandlerImpl implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        String[] parameters = new String[method.getParameters().length];
        int index = 0;
        for (Parameter parameter : method.getParameters()){
            parameters[index] = parameter.getParameterizedType().getTypeName();
            index++;
        }
        SqlParameterStatement sqlParameterStatement = new SqlParameterStatement(new SimpleDataSource().getConnection(),
                method.getAnnotation(Query.class).value());
        sqlParameterStatement.setQuery(parameters,args);
        ResultSet resultSet = sqlParameterStatement.excute();

        Object o = sqlParameterStatement.returnConvert(resultSet, method.getGenericReturnType().getTypeName());
        sqlParameterStatement.close();
        return o;
    }

}
